package com.sc.nft.controller;

import com.sc.nft.annotation.UserLogin;
import com.sc.nft.controller.request.IdRequest;
import com.sc.nft.controller.request.PrivilegeCollectionRequest;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.vo.*;
import com.sc.nft.enums.RedisKeyEnum;
import com.sc.nft.exception.GlobalException;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.DigitalCollectionProductInfoService;
import com.sc.nft.service.PrivilegeCollectionService;
import com.sc.nft.service.PrivilegeRuleService;
import com.sc.nft.sup.ErrorCode;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/privilege")
@Api(tags="合成玩法")
public class PrivilegeCollectionController {
    private final PrivilegeCollectionService privilegeCollectionService;
    private final Redisson redisson;
    @PostMapping(value="/privilegeCollection", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "可合成的商品列表",notes ="可合成的商品列表",httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<List<PrivilegeProductVO>> privilegeCollection(@Valid @RequestBody PrivilegeCollectionRequest request){
        List<PrivilegeProductVO>list= privilegeCollectionService.privilegeProductList(request.getCommodityType());
        return Result.success(list);
    }

    @PostMapping(value="/privilegeCollectionDetail", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "可合成特权商品详情",notes ="可合成特权商品详情",httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin(required = false)
    public Result <PrivilegeProductDetailVO> privilegeCollectionDetail(@Valid @RequestBody IdRequest request){
        PrivilegeProductDetailVO privilegeProductDetailVO= privilegeCollectionService.privilegeProductDetail(request.getId());
        return Result.success(privilegeProductDetailVO);
    }
    @PostMapping(value="/synthesisCollectionDetail", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "可合成商品详情",notes ="可合成商品详情",httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin(required = false)
    public Result <SynthesisCollectionDetailVO> synthesisCollectionDetail(@Valid @RequestBody IdRequest request){
        SynthesisCollectionDetailVO synthesisCollectionDetailVO= privilegeCollectionService.synthesisCollectionDetail(request.getId());
        return Result.success(synthesisCollectionDetailVO);
    }
    @PostMapping(value="/synthesisPrivilege", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "合成商品",notes ="合成商品",httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<SynthesisPrivilegeVO>  synthesisPrivilege(@Valid @RequestBody IdRequest request) throws GlobalException {
        RLock lock = redisson.getLock(RedisKeyEnum.SYNTHESIS_PRIVILEGE.getKey()+request.getId());
        SynthesisPrivilegeVO synthesisPrivilegeVO=new SynthesisPrivilegeVO();
        try{
            boolean res = lock.tryLock(30, TimeUnit.SECONDS);
            if (res) {
                 synthesisPrivilegeVO= privilegeCollectionService.synthesisPrivilege(request.getId());
            }else{
                return Result.fail(ErrorCode.LATER_TRY);
            }
        } catch (InterruptedException e) {
            throw new GlobalException(ErrorCode.LATER_TRY);
        } finally {
            if(lock.isLocked() && lock.isHeldByCurrentThread()){ // 是否还是锁定状态  // 时候是当前执行线程的锁
                lock.unlock(); // 释放锁
            }
        }
        return Result.success(synthesisPrivilegeVO);
    }

    @PostMapping(value="/synthesisRecord", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "合成记录",notes ="合成记录",httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<List<SynthesisRecordVO>>  synthesisRecord() {
        List<SynthesisRecordVO>list= privilegeCollectionService.synthesisRecord();
        return Result.success(list);
    }
}
